package controllers

import (
	"xiegao/caches"
	"xiegao/models"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type HorseMan struct {
	beego.Controller
}

// @Title 骑手
// @Description 骑手列表
// @Params name string path false 骑手姓名
// @Params place string path false 骑手位置
// @Success 200 {object} models.horseman.HorseMans
// @router /horseman [get]
func (self *HorseMan) GetList() {
	name := self.GetString("name")
	place := self.GetString("place")

	orm := orm.NewOrm()
	var hourse_mans []models.HorseMans
	orm.QueryTable("horse_mans").Filter("name__icontains", name).Filter("home_place__icontains", place).All(&hourse_mans)

	self.Data["json"] = map[string]interface{}{
		"code": 200,
		"data": hourse_mans,
		"msg":  "获取成功",
	}
	self.ServeJSON()
	self.StopRun()

}

func (self *HorseMan) Create() {

}

func (self *HorseMan) GetOne() {

}

func (self *HorseMan) DeleteOne() {

}

func (self *HorseMan) UpdateOne() {

}

// @Title 更新骑手位置
// @Description 更新骑手位置
// @Params longitude string body true 经度
// @Params latitude string body true 纬度
// @Success {"code":200,"msg":"更新成功"}
// @router /horseman/local [patch]
func (self *HorseMan) UpdateLocalPlace() {
	id, _ := self.GetInt("id")
	longitude, _ := self.GetFloat("longitude")
	latitude, _ := self.GetFloat("latitude")

	local := caches.NewLocal()
	res := local.SetLocal(id, longitude, latitude)
	if res {
		self.Data["json"] = map[string]interface{}{
			"code": 200,
			"msg":  "更新成功",
		}
		self.ServeJSON()
		self.StopRun()
	} else {
		self.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "更新失败",
		}
		self.ServeJSON()
		self.StopRun()
	}

}

// @Title 获取骑手位置
// @Description 获取骑手位置
// @Params id string path true 经度
// @Success {"code":200,"msg":"获取成功","data":{"longitude":"longitude","latitude":"latitude"}}
// @router /horseman/local/:id [get]
func (self *HorseMan) GetLocalPlace() {
	id, _ := self.GetInt("id")

	local := caches.NewLocal()
	res, data := local.GetLocal(id)
	if res {
		self.Data["json"] = map[string]interface{}{
			"code": 200,
			"data": data,
			"msg":  "获取成功",
		}
		self.ServeJSON()
		self.StopRun()
	} else {
		self.Data["json"] = map[string]interface{}{
			"code": 404,
			"msg":  "未找到数据",
		}
		self.ServeJSON()
		self.StopRun()
	}

}
